package com.banciyuan.bcywebview.biz.novel;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.push.PushDataHelper;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.DetailDataHelper;
import com.banciyuan.bcywebview.base.view.tag.TagView;
import com.banciyuan.bcywebview.biz.circles.smooth.CircleSmoothActivity;
import com.banciyuan.bcywebview.biz.circles.smooth.CircleTagSmoothActivity;
import com.banciyuan.bcywebview.biz.event.EventDetailActivity;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.HotEvent;
import de.greenrobot.daoexample.model.Novel;
import de.greenrobot.daoexample.model.TagDetail;


/**
 * Created by hy on 2015/5/15.
 */
public class NovelAdapter extends BaseAdapter {

    private List<Novel> data = new ArrayList<>();
    private Context context;

    public NovelAdapter(Context context, List<Novel> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getItemViewType(int position) {
        if (data.get(position).getEvents() != null && !data.get(position).getEvents().isEmpty()) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        final Novel novel = data.get(i);
        ViewHolder mViewHolder = null;
        EventHolder mEventHolder = null;
        if (null == convertView) {
            if (getItemViewType(i) == 0) {
                convertView = View.inflate(context, R.layout.new_discover_novel_item, null);
                mViewHolder = new ViewHolder(convertView);
                convertView.setTag(mViewHolder);
            } else if (getItemViewType(i) == 1) {
                convertView = View.inflate(context, R.layout.novel_recommend_item, null);
                mEventHolder = new EventHolder(convertView);
                convertView.setTag(mEventHolder);
            }
        } else {
            if (getItemViewType(i) == 0) {
                mViewHolder = (ViewHolder) convertView.getTag();
            } else if (getItemViewType(i) == 1) {
                mEventHolder = (EventHolder) convertView.getTag();
            }
        }
        if (getItemViewType(i) == 0 && mViewHolder != null) {
            if (!TextUtils.isEmpty(novel.getCover())) {
                ImageLoader.getInstance().displayImage(novel.getCover(), mViewHolder.cover, ImageOptions.options_noneDisplay);
            }
            String name = "";
            if (novel.getUname().length() > 10) {
                name = novel.getUname().substring(0, 10) + "...";
            } else {
                name = novel.getUname();
            }
            if (novel.isPost_in_set()) {
                mViewHolder.tvName.setText(String.format(context.getString(R.string.novel_format), name, context.getResources().getString(R.string.series)));
            } else {
                mViewHolder.tvName.setText(String.format(context.getString(R.string.novel_format), name, context.getResources().getString(R.string.series_single)));
            }
            if (!TextUtils.isEmpty(novel.getTitle())) {
                mViewHolder.tvTitle.setText(Html.fromHtml(novel.getTitle()));
            }
            String intro = novel.getIntro();
            if (TextUtils.isEmpty(intro)) {
                mViewHolder.tvDesc.setVisibility(View.GONE);
            } else {
                mViewHolder.tvDesc.setVisibility(View.VISIBLE);
                mViewHolder.tvDesc.setText(Html.fromHtml(novel.getIntro()));
            }
            processTagData(mViewHolder, novel, 0);
            ((View) (mViewHolder.cover.getParent())).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DetailDataHelper.gotoWriterDetail(context, novel.getRp_id(), novel.getWp_id(), false);
                }
            });
        } else if (getItemViewType(i) == 1 && mEventHolder != null) {
            for (int j = 0; j < novel.getEvents().size(); j++) {
                final HotEvent h = novel.getEvents().get(j);
                ImageLoader.getInstance().displayImage(h.getPath(), mEventHolder.covers[j], ImageOptions.options_noneDisplay);
                if (!TextUtils.isEmpty(h.getTitle())) {
                    mEventHolder.unames[j].setText(Html.fromHtml(h.getTitle()));
                }
                mEventHolder.labels[j].setVisibility(View.VISIBLE);
                mEventHolder.zan_counts[j].setText(context.getString(R.string.check_event_detail));
                mEventHolder.zan_ivs[j].setVisibility(View.GONE);
                mEventHolder.container[j].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        PushDataHelper.processInnerPushData(context, h.getCode());
                    }
                });
            }
        }
        return convertView;
    }

    private void processTagData(ViewHolder viewHolder, final Novel novel, final int from) {
        if (novel.getPost_tags() != null && novel.getPost_tags().size() > 0) {
            viewHolder.tags.setVisibility(View.VISIBLE);
        } else {
            viewHolder.tags.setVisibility(View.GONE);
        }
        if (novel.getPost_tags() != null && novel.getPost_tags().size() > 0) {
            viewHolder.tags.setTlist(novel.getPost_tags());
            viewHolder.tags.setTagViewClick(new TagView.OnTagClickListener() {
                @Override
                public void onClick(TagDetail t) {
                    if (!TextUtils.isEmpty(t.getWork())) {
                        if (!TextUtils.isEmpty(t.getWid())) {
                            Intent intent = new Intent(context, CircleSmoothActivity.class);
                            intent.putExtra(gotoUtil.INTENT_VALUE_ONE, t.getWork());
                            intent.putExtra(gotoUtil.INTENT_VALUE_TWO, t.getWid());
                            intent.putExtra("from", from);
                            context.startActivity(intent);
                        }
                    } else if (!TextUtils.isEmpty(t.getEvent_id())) {
                        gotoUtil.gotoAct(context, EventDetailActivity.class, t.getEvent_id());
                    } else {
                        Intent intent = new Intent(context, CircleTagSmoothActivity.class);
                        intent.putExtra(gotoUtil.INTENT_VALUE_ONE, t.getTag_name());
                        intent.putExtra("type", "tag");
                        intent.putExtra("from", from);
                        context.startActivity(intent);
                    }
                }
            });
        }
    }

    private class ViewHolder {
        ImageView cover;
        TextView tvName;
        TextView tvTitle;
        TextView tvDesc;
        TagView tags;

        public ViewHolder(View convertView) {
            cover = (ImageView) convertView.findViewById(R.id.novel_cover);
            tvName = (TextView) convertView.findViewById(R.id.tv_novel_name);
            tvTitle = (TextView) convertView.findViewById(R.id.tv_novel_title);
            tvDesc = (TextView) convertView.findViewById(R.id.tv_novel_desc);
            tags = (TagView) convertView.findViewById(R.id.tags);
        }
    }

    private class EventHolder {
        public ImageView[] covers;
        public TextView[] unames;
        public ImageView[] zan_ivs;
        public TextView[] zan_counts;
        public ImageView[] labels;
        public LinearLayout[] container;


        public EventHolder(View convertView) {
            covers = new ImageView[]{(ImageView) convertView.findViewById(R.id.cover_one),
                    (ImageView) convertView.findViewById(R.id.cover_two)};
            unames = new TextView[]{(TextView) convertView.findViewById(R.id.uname_one),
                    (TextView) convertView.findViewById(R.id.uname_two)};
            zan_ivs = new ImageView[]{(ImageView) convertView.findViewById(R.id.zan_label_one),
                    (ImageView) convertView.findViewById(R.id.zan_label_two)};
            zan_counts = new TextView[]{(TextView) convertView.findViewById(R.id.zan_count_one),
                    (TextView) convertView.findViewById(R.id.zan_count_two)};
            container = new LinearLayout[]{(LinearLayout) convertView.findViewById(R.id.container_one),
                    (LinearLayout) convertView.findViewById(R.id.container_two)};
            labels = new ImageView[]{(ImageView) convertView.findViewById(R.id.cover_label_one),
                    (ImageView) convertView.findViewById(R.id.cover_label_two)};
        }
    }
}
